# +----------------------------------------------------------------------
# | Pink [ A modern python web framework ]
# +----------------------------------------------------------------------
# | Copyright (c) 2023 http:#unnnnn.com All rights reserved.
# +----------------------------------------------------------------------
# | Author: chenjianhua <unnnnn@foxmail.com>
# +----------------------------------------------------------------------

"""New Key Command."""
from cryptography.fernet import Fernet

from .Command import Command
from cleo.io.inputs.option import Option as option


class KeyCommand(Command):
    name = 'key'
    description = 'Generate a new key.'
    help = """
     {--s|--store : Stores the key in the .env file}
     {--d|--dont-store : Does not store the key in the .env file}
    """
    options = [
        option('store', 's', flag=True,
               description='Stores the key in the .env file'),
        option('dont-store', 'd', flag=True,
               description='Does not store the key in the .env file'),
    ]

    """
    Generate a new key.

    key
        {--s|--store : Stores the key in the .env file}
        {--d|--dont-store : Does not store the key in the .env file}
    """

    def handle(self):
        key = bytes(Fernet.generate_key()).decode("utf-8")

        if self.option("dont-store"):
            return self.info(f"Key: {key}")

        with open(".env", "r") as file:
            data = file.readlines()

        for line_number, line in enumerate(data):
            if line.startswith("APP_KEY="):
                data[line_number] = "APP_KEY={}\n".format(key)
                break

        with open(".env", "w") as file:
            file.writelines(data)

        self.info("Key added to your .env file: {}".format(key))
